package com.smart.manager.member.repository;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ObjUtil;
import com.smart.common.entities.core.Page;
import com.smart.manager.member.entities.bodys.RMemberMaterialPigPlan;
import com.smart.manager.member.entities.enums.PigPlanStatus;
import com.smart.manager.member.entities.enums.PigPlanTime;
import com.smart.manager.member.entities.models.MemberMaterialPig;
import com.smart.manager.member.entities.models.MemberMaterialPigPlan;
import com.smart.manager.member.entities.models.query.QMemberMaterialPig;
import com.smart.manager.member.entities.models.query.QMemberMaterialPigPlan;
import com.smart.manager.member.entities.views.MemberPigView;
import com.smart.manager.product.entities.enums.CameraDirection;
import com.smart.manager.product.entities.enums.PigStatus;
import com.smart.manager.product.entities.models.BasicCameraDetail;
import com.smart.manager.product.entities.models.PropsCombo;
import com.smart.manager.product.entities.models.query.QBasicCameraDetail;
import com.smart.manager.product.entities.views.PropsComboView;
import com.smart.manager.product.repository.BasicCameraDetailRepository;
import io.ebean.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


/**
 * 玩家猪猪- 喂食计划
 * @author leo
 * @date 2023/09/22
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class MemberMaterialPigPlanRepository {


    private final BasicCameraDetailRepository basicCameraDetailRepository;


    public Page<MemberMaterialPigPlan> pageList(RMemberMaterialPigPlan.SearchBody search) {
        QMemberMaterialPigPlan query = new QMemberMaterialPigPlan();
        Opt.ofBlankAble(search.getPhoneNumber()).ifPresent(x -> query.member.phoneNumber.contains(x));
        Opt.ofBlankAble(search.getPigId()).ifPresent(x -> query.pigId.id.eq(x));
        Opt.ofBlankAble(search.getCombo()).ifPresent(x -> query.combo.code.eq(x));
        Opt.ofBlankAble(search.getStatus()).ifPresent(x -> query.status.eq(x));
        Opt.ofBlankAble(search.getWorkOrder()).ifPresent(x -> query.workOrder.contains(x));
        Opt.ofBlankAble(search.getEatDate()).ifPresent(x -> query.eatDate.eq(x));
        Opt.ofBlankAble(search.getEatTime()).ifPresent(x -> query.eatTime.eq(x));
        query.order().eatDate.asc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<MemberMaterialPigPlan>().load(query.findPagedList());
    }

    /**
     * 获取我的猪仔
     * @param id 会员id
     * @return {@link List}<{@link MemberMaterialPig}>
     */
    public List<MemberMaterialPig> getOneList(Long id,Boolean combine){
        QMemberMaterialPig sel = new QMemberMaterialPig();
        if (combine){
            sel.closeMember.isNotNull().closeMember.id.eq(id);
        }else {
            sel.member.id.eq(id);
        }
        sel.or().status.eq(PigStatus.NORMAL).status.eq(PigStatus.WAIT).endOr();
        List<MemberMaterialPig> pigs = sel.findList();
        for (MemberMaterialPig pig : pigs) {
            if (ObjUtil.isNotEmpty(pig.getPigsty())) {
                List<String> cameraCode = pig.getPigsty().getCameraCode();
                List<BasicCameraDetail> details = new QBasicCameraDetail().passageId.in(cameraCode).findList();
                CollUtil.sort(details, Comparator.comparing(BasicCameraDetail::getCameraDirection, (direction1, direction2) -> {
                    if (direction1.equals(CameraDirection.FORMER)) {
                        return -1; // 如果direction1等于"前摄像头"，则排在前面
                    } else if (direction2.equals(CameraDirection.FORMER)) {
                        return 1; // 如果direction2等于"前摄像头"，则排在前面
                    } else {
                        return 0; // 其他情况不变
                    }
                }));
                ArrayList<String> strings = new ArrayList<>();
                details.forEach(x->{
                    strings.add(basicCameraDetailRepository.getCameraUrl(x,false));
                });
                pig.setPigsty(pig.getPigsty().setCameraUrl(strings));
            }
        }
        return pigs;

    }


    /**
     * 新增一天喂食计划
     * @param pig 猪
     * @param combo 套餐
     * @param eatData 时间
     */
    @Transactional(rollbackFor = Exception.class)
    public void add (MemberMaterialPig pig, PropsCombo combo, LocalDate eatData) {
        List<PigPlanTime> times = List.of(PigPlanTime.L1, PigPlanTime.L2, PigPlanTime.L3);
        for (PigPlanTime time : times) {
            new MemberMaterialPigPlan()
                    .setMember(pig.getMember())
                    .setPigId(new MemberPigView()
                            .setId(pig.getId()))
                    .setStatus(PigPlanStatus.L2)
                    .setCombo(new PropsComboView()
                            .setCode(combo.getCode()))
                    .setEatDate(eatData)
                    .setEatTime(time)
                    .insert();
        }

    }

    /**
     * 新增一天喂食计划
     * @param pig 猪
     * @param combo 套餐
     * @param eatData 时间
     */
    public void add (MemberMaterialPig pig, PropsCombo combo, LocalDate eatData,List<PigPlanTime> times ) {
        for (PigPlanTime time : times) {
            new MemberMaterialPigPlan()
                    .setMember(pig.getMember())
                    .setPigId(new MemberPigView()
                            .setId(pig.getId()))
                    .setStatus(PigPlanStatus.L2)
                    .setCombo(new PropsComboView()
                            .setCode(combo.getCode()))
                    .setEatDate(eatData)
                    .setEatTime(time)
                    .insert();
        }

    }
}
